package com.smart.wap.app;


import cn.hutool.core.lang.Assert;

import com.alibaba.fastjson2.JSONObject;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.enums.SmsScene;
import com.smart.manager.member.entities.models.MemberProfileMain;
import com.smart.manager.member.repository.MemberProfileMainRepository;
import com.smart.wap.bodys.RegisterBody;
import com.smart.wap.service.MemberRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaxuqiu
 * @date 2023/09/10
 */
@Api(tags = "移动端-注册相关")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "app/register")
@RequiredArgsConstructor
@Log4j2
public class RegisterController {

    private final MemberRegisterService memberRegisterService;
    private final MemberProfileMainRepository memberProfileMainRepository;

    @PostMapping(value = "getSmsCode")
    @ApiOperation(value = "获取注册验证码", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<String> getSmsCode(@RequestBody @Validated RegisterBody.CaptchaBody body) {
        memberRegisterService.getSmsCode(SmsScene.MEMBER_REGISTER, body);
        return AjaxResult.success();
    }


    @PostMapping(value = "action")
    @ApiOperation(value = "邀请注册处理程序", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<String> actionRegister(@RequestBody @Validated RegisterBody.RegBody body) {
        memberRegisterService.actionRegister(body);
        return AjaxResult.success();
    }


    @GetMapping(value = "getInviterInfo/{id}")
    @ApiOperation(value = "获取邀请人信息", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<JSONObject> getInviterInfo(@PathVariable Long id) {
        MemberProfileMain record = memberProfileMainRepository.getOneById(id);
        Assert.notNull(record, "推广员信息不存在");
        JSONObject result = JSONObject.of("avatar", record.getAvatar(), "nickName", record.getNickName(), "inviterId", record.getId());
        return AjaxResult.data(result);
    }


}
